package top.lywivan.mm.service.imp;

import top.lywivan.mm.dao.CourseDao;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.pojo.Course;
import top.lywivan.mm.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;


    @Override
    @Transactional(readOnly = true)
    public PageResult findListByPage(QueryPageBean queryPageBean) {

        Integer pageSize = queryPageBean.getPageSize();
        Map queryParams = queryPageBean.getQueryParams();
        Course course = new Course();
        if(queryParams!=null&&queryParams.size()!=0){
            if(queryParams.get("name")!=null){
                course.setName("%"+queryParams.get("name")+"%");
            }
            course.setIsShow((Integer) queryParams.get("status"));
        }
        Long count = courseDao.findTotalCount(course);
        List<Course> list = courseDao.findListByPage(queryPageBean.getOffset(), pageSize, course);
        return new PageResult(count,list);
    }

    @Override
    @Transactional
    public void addCourse(Course course) {
        courseDao.addCourse(course);
    }

    @Override
    @Transactional
    public void updateCourse(Course course) {
        courseDao.updateCourse(course);
    }

    @Override
    @Transactional
    public void deleteCourseById(Integer id) {
        courseDao.deleteCourseById(id);
    }

    @Override
    public List<Course> findListAllCourse() {
        return courseDao.findListAllCourse();
    }
}
